package com.example.administrator.u17.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.db.MyData;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class InputActivity extends AppCompatActivity {

    private ListView mListView;
    //    private AutoCompleteTextView mTextView;
    private EditText mTextView;
//    private ArrayAdapter<String> mArrayAdapter;
    private ArrayAdapter<String> mListAdapter;
    private List<String> mList, mData;
    private DbManager mManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_input);
        mManager = x.getDb(new DbManager.DaoConfig());
        initView();
        initData();
        setData();
        setListener();
    }

    private void setData() {
        mListView.setAdapter(mListAdapter);
    }

    private void setListener() {
//        mTextView.addTextChangedListener(new TextWatcher() {
//            /**
//             * 内容改变之前调用
//             * 参数说明
//             * s：改变之前的内容
//             * count:减少的字符个数
//             * after:新增的字符个数
//             * start:开始改变的字符的索引
//             * */
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            /**
//             * 文本内容改变调用
//             * 参数说明：
//             * s:当前的编辑框内容
//             * start:开始改变的字符的索引
//             * before:减少的字符个数
//             * count:新增的字符个数*/
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                mList.add(s.toString());
//                mArrayAdapter = new ArrayAdapter<String>(InputActivity.this, android.R.layout.simple_spinner_item, mList);
//                mTextView.setAdapter(mArrayAdapter);
//            }
//        });
//
//        mTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(InputActivity.this, SelectedActivity.class);
//                try {
//                    String content = URLEncoder.encode(mArrayAdapter.getItem(position).toString(), "UTF-8");
//                    intent.putExtra("name", content);
//                    startActivity(intent);
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(InputActivity.this, SelectedActivity.class);
                try {
                    String content = URLEncoder.encode(mData.get(position).toString(), "UTF-8");
                    intent.putExtra("name", content);
                    startActivity(intent);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
    }



    private void initData() {
        mList = new ArrayList<>();
        mData = new ArrayList<>();
        mListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mData);
        try {
//            Toast.makeText(this, mManager.selector(MyData.class).where("json","=","").findAll().size(), Toast.LENGTH_SHORT).show();
            List<MyData> data = mManager.selector(MyData.class).findAll();
            if (data != null) {
                for (int i = 0; i < data.size(); i++) {
                      mData.add(data.get(i).json);
                }
                mListAdapter.notifyDataSetChanged();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }




    }

    private void initView() {
        mTextView = (EditText) findViewById(R.id.et_input_activity);
        mListView = (ListView) findViewById(R.id.lv_input_activity);

    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_input_activity:
                finish();
                break;
            case R.id.iv_search_input_activity:
                if (!TextUtils.isEmpty(mTextView.getText().toString())) {
                    MyData myData = new MyData();
                    myData.json = mTextView.getText().toString();
                    myData.time=System.currentTimeMillis();
                    try {
                        mManager.save(myData);
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }

                Intent intent = new Intent(InputActivity.this, SelectedActivity.class);
                try {
                    String content = URLEncoder.encode(mTextView.getText().toString(), "UTF-8");
                    intent.putExtra("name", content);
                    startActivity(intent);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_delete_input_activity:

                mData.clear();
                mListAdapter.notifyDataSetChanged();
                try {
                    mManager.delete(MyData.class);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.iv_delete_input_activity:
                mTextView.setText("");
                break;
        }
    }


}
